#pragma once

using namespace System::Windows;
using namespace System::ComponentModel;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Resources;

#include "Utility.h"
#include "ActionMenuRecord.h"
#include "UtilityChooser.h"
#include "ImageKeeper.h"


namespace ttg_internal
{
	namespace utils
	{
		/// <summary>
		/// Summary for MainForm
		/// </summary>
		public ref class MainForm : public System::Windows::Forms::Form
		{
			private:
				ImageKeeper ^_ikeeper;
				///<summary>
				/// Generates items for WinForms menu according to records.
				/// Note: it does not add default records (such as "Close", "Hide", etc).
				///<summary>
				static Forms::ToolStripItem ^Convert(ActionMenuRecord ^record)
				{
					Forms::ToolStripMenuItem ^mi = nullptr;
					//It's leaf-record.
					if (record->Childs->Count == 0)
					{
						mi = gcnew Forms::ToolStripMenuItem(record->Name, record->Image, record->Handler);
					}
					//It's filter.
					else
					{
						List<Forms::ToolStripItem ^> ^childs
							= gcnew List<Forms::ToolStripItem ^>();
						for each (ActionMenuRecord ^r in record->Childs)
							childs->Add(Convert(r));
						mi = gcnew Forms::ToolStripMenuItem(record->Name,
															record->Image,
															childs->ToArray());
					}
					mi->Text = record->Name;
					return mi;
				}

				static void HideShowHandler(System::Object ^sender, System::EventArgs ^args)
				{
					try
					{
						UtilityPlusController ^upc
							= safe_cast<UtilityPlusController ^>(safe_cast<Forms::ToolStripItem ^>(sender)->Tag);
						upc->Utility->ActivateOrDeActivate();
					}
					catch (System::Exception ^ex)
					{
						Forms::MessageBox::Show(gcnew System::String("Internal error: ") + ex->ToString(),
												"Internal error");
					}
				}

				///<summary>
				/// Generates root ToolStripItem for utility.
				///<summary>
				Forms::ToolStripItem ^GenerateToolStrip(UtilityPlusController ^upc)
				{
					//Creating childs.
					List<Forms::ToolStripItem ^> ^childs
						= gcnew List<Forms::ToolStripItem ^>();
					for each (ActionMenuRecord ^r in upc->Controller->Records)
						childs->Add(Convert(r));
					if (childs->Count != 0)
						childs->Add(gcnew Forms::ToolStripSeparator());
					Forms::ToolStripMenuItem ^mi = gcnew Forms::ToolStripMenuItem(
																	"Hide/Show",
																	nullptr,
																	gcnew System::EventHandler(HideShowHandler));
					mi->Tag = upc;
					childs->Add(mi);
					//Creating result item.
					Forms::ToolStripMenuItem ^res
						= gcnew Forms::ToolStripMenuItem(upc->Utility->Name,
														 _ikeeper->UtilityOn,
														 childs->ToArray());
					res->Tag = upc;
					return res;
				}
		public:
			MainForm(void)
			{
				InitializeComponent();
				_ikeeper = gcnew ImageKeeper();
				notifyIcon->Icon = _ikeeper->ttgIcon;
			}

			Forms::NotifyIcon ^GetIcon()
			{
				return notifyIcon;
			}
		internal:
			void SetActionMenu(UtilityPlusController ^upc)
			{
				contextMenuStrip->SuspendLayout();
				contextMenuStrip->Items->Insert(0, GenerateToolStrip(upc));
				contextMenuStrip->ResumeLayout();
				contextMenuStrip->PerformLayout();
			}

		protected:
			/// <summary>
			/// Clean up any resources being used.
			/// </summary>
			~MainForm()
			{
				if (components)
				{
					delete components;
				}
			}
		private: System::Windows::Forms::Label^  label;
		private: System::Windows::Forms::NotifyIcon^  notifyIcon;
		private: System::Windows::Forms::ContextMenuStrip^  contextMenuStrip;
		private: System::Windows::Forms::ToolStripMenuItem^  muClose;
		private: System::Windows::Forms::ToolStripSeparator^  ttSeparator1;
private: System::Windows::Forms::ToolStripMenuItem^  muLoad;
private: System::Windows::Forms::Timer^  timer500MS;






		private: System::ComponentModel::IContainer^  components;
		protected: 

		protected: 

		private:
			/// <summary>
			/// Required designer variable.
			/// </summary>


	#pragma region Windows Form Designer generated code
			/// <summary>
			/// Required method for Designer support - do not modify
			/// the contents of this method with the code editor.
			/// </summary>
			void InitializeComponent(void)
			{
				this->components = (gcnew System::ComponentModel::Container());
				this->label = (gcnew System::Windows::Forms::Label());
				this->notifyIcon = (gcnew System::Windows::Forms::NotifyIcon(this->components));
				this->contextMenuStrip = (gcnew System::Windows::Forms::ContextMenuStrip(this->components));
				this->ttSeparator1 = (gcnew System::Windows::Forms::ToolStripSeparator());
				this->muLoad = (gcnew System::Windows::Forms::ToolStripMenuItem());
				this->muClose = (gcnew System::Windows::Forms::ToolStripMenuItem());
				this->timer500MS = (gcnew System::Windows::Forms::Timer(this->components));
				this->contextMenuStrip->SuspendLayout();
				this->SuspendLayout();
				// 
				// label
				// 
				this->label->AutoSize = true;
				this->label->Location = System::Drawing::Point(31, 9);
				this->label->Name = L"label";
				this->label->Size = System::Drawing::Size(97, 13);
				this->label->TabIndex = 0;
				this->label->Text = L"Under construction";
				// 
				// notifyIcon
				// 
				this->notifyIcon->ContextMenuStrip = this->contextMenuStrip;
				this->notifyIcon->Text = L"ttgLib Manager";
				this->notifyIcon->Visible = true;
				// 
				// contextMenuStrip
				// 
				this->contextMenuStrip->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->ttSeparator1, 
					this->muLoad, this->muClose});
				this->contextMenuStrip->Name = L"contextMenuStrip";
				this->contextMenuStrip->RenderMode = System::Windows::Forms::ToolStripRenderMode::System;
				this->contextMenuStrip->Size = System::Drawing::Size(138, 54);
				// 
				// ttSeparator1
				// 
				this->ttSeparator1->Name = L"ttSeparator1";
				this->ttSeparator1->Size = System::Drawing::Size(134, 6);
				// 
				// muLoad
				// 
				this->muLoad->Name = L"muLoad";
				this->muLoad->Size = System::Drawing::Size(137, 22);
				this->muLoad->Text = L"Load utility";
				this->muLoad->Click += gcnew System::EventHandler(this, &MainForm::loadToolStripMenuItem_Click);
				// 
				// muClose
				// 
				this->muClose->Enabled = false;
				this->muClose->Name = L"muClose";
				this->muClose->Size = System::Drawing::Size(137, 22);
				this->muClose->Text = L"Close";
				this->muClose->ToolTipText = L"Close manager and all utilities.";
				// 
				// timer500MS
				// 
				this->timer500MS->Enabled = true;
				this->timer500MS->Interval = 500;
				this->timer500MS->Tick += gcnew System::EventHandler(this, &MainForm::timer500MS_Tick);
				// 
				// MainForm
				// 
				this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
				this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
				this->ClientSize = System::Drawing::Size(157, 38);
				this->Controls->Add(this->label);
				this->Enabled = false;
				this->Name = L"MainForm";
				this->Text = L"MainForm";
				this->WindowState = System::Windows::Forms::FormWindowState::Minimized;
				this->Load += gcnew System::EventHandler(this, &MainForm::MainForm_Load);
				this->Shown += gcnew System::EventHandler(this, &MainForm::MainForm_Shown);
				this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &MainForm::MainForm_FormClosing);
				this->contextMenuStrip->ResumeLayout(false);
				this->ResumeLayout(false);
				this->PerformLayout();

			}
	#pragma endregion
		private: System::Void MainForm_Shown(System::Object^  sender, System::EventArgs^  e)
				 {
					 Hide();
				 }
		private: System::Void MainForm_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e)
				 {
					 notifyIcon->Visible::set(false);
				 }
private: System::Void MainForm_Load(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void loadToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e)
		 {
			 (gcnew UtilityChooser())->ShowDialog();
		 }
private: System::Void timer500MS_Tick(System::Object^  sender, System::EventArgs^  e)
		 {
			 if (contextMenuStrip->Visible)
			 {
				 try
				 {
					 contextMenuStrip->SuspendLayout();
					 for each (Forms::ToolStripItem ^item in contextMenuStrip->Items)
					 {
						try
						{
							UtilityPlusController ^upc = safe_cast<UtilityPlusController ^>(item->Tag);
							if (upc != nullptr)
							{
								item->Image = upc->Utility->IsActive() 
											? _ikeeper->UtilityOn
											: _ikeeper->UtilityOff;
							}
						}
						catch (System::Exception ^) { }
					 }
					 contextMenuStrip->ResumeLayout();
					 contextMenuStrip->PerformLayout();
				 } catch (System::Exception ^) { }
			 }
		 }
};
	}
}
